package com.aphrodite.ui.widgets;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;

/**
 * Spinner item with that uses predefined contents.
 * 
 * @author kentelt
 * 
 */
public class Spinner extends TraversableWidget {

	private final static int spinrArwHeight = WidgetHelper.arrowHeight * 2 + 9;
	private final static int spinrArwWidth = WidgetHelper.arrowWidth + 8;

	volatile int currentIndex;

	Vector items = new Vector();

	public Spinner(Theme theme) {
		super(theme, 0, 0);
		updateModel();
	}

	public void addItem(String text) {
		items.addElement(text);
		updateModel();
	}

	void decreaseIndex() {
		if (currentIndex > 0)
			currentIndex--;
		reDrawWidget();
	}

	public int getSelectedIndex() {
		return currentIndex;
	}

	public String getSelectedItem() {
		return (String) items.elementAt(currentIndex);
	}

	void increaseIndex() {
		if (currentIndex < items.size() - 1)
			currentIndex++;
		reDrawWidget();
	}

	public boolean moveDown() {
		decreaseIndex();
		return false;
	}

	public boolean moveUp() {
		increaseIndex();
		return false;
	}

	public void removeItem(int index) {
		removeItem((String) items.elementAt(index));
		updateModel();
		reDrawWidget();
		stateChanged(this);
	}

	public void removeItem(String text) {
		items.removeElement(text);
		updateModel();
	}

	public void render(Graphics g) {
		if (!isVisible)
			return;
		final int actW = width + 2 - spinrArwWidth;
		final int spinHt = height / 2;
		capture(g, actW, height, true);
		if (items.size() > 0) {
			g.setColor(theme.fgColor);
			if (theme.bmFont == null) {
				g.setFont(theme.widgetFont);
				g.drawString((String) items.elementAt(currentIndex), 2, spinHt - theme.widgetFont.getHeight() / 2, 0);
			} else {
				theme.bmFont.drawString(g, (String) items.elementAt(currentIndex), 2, spinHt
						- theme.widgetFont.getHeight() / 2, 0);
			}
		}
		g.setClip(actW, 0, spinrArwWidth + 4, height);
		g.setColor(theme.bgColor);
		g.fillRoundRect(actW - 10, 0, spinrArwWidth + 10, height, 8, 8);
		g.setColor(theme.borderColor);
		g.drawRoundRect(actW - 10, 0, spinrArwWidth + 10, height - 1, 8, 8);
		g.drawLine(actW + 1, spinHt, actW + spinrArwWidth, spinHt);
		WidgetHelper.drawSpinnerArrows(g, actW + 4, spinHt - WidgetHelper.arrowHeight / 2, LAYOUT_TOP);
		WidgetHelper
				.drawSpinnerArrows(g, actW + 4, spinHt + (spinHt / 2 - WidgetHelper.arrowHeight / 2), LAYOUT_BOTTOM);
		g.setClip(0, 0, actW + 2, height);
		release(g, actW + 2, height, true);
	}

	/**
	 * Overridden function. Spinner cannot have a title bar.
	 */
	public final void setTitle(String title) {}

	protected boolean traverse(int dir, int viewportWidth, int viewportHeight, int[] visRect_inout) {
		return false;
	}

	protected void updateModel() {
		int maxWidth = 0;
		int tmpWidth = 0;
		/* Find the longest string in the spinner to set the width */
		for (int i = 0; i < items.size(); i++) {
			if (theme.bmFont == null) {
				tmpWidth = theme.widgetFont.stringWidth((String) items.elementAt(i));
			} else {
				tmpWidth = theme.bmFont.stringWidth((String) items.elementAt(i));
			}
			if (tmpWidth > maxWidth)
				maxWidth = tmpWidth;
		}
		/* set the width of the spinner, 20 is the width of the spinner arrows */
		width = maxWidth + 2 + spinrArwWidth;
		height = theme.bmFont == null ? theme.widgetFont.getHeight() + 9 : theme.bmFont.getHeight() + 9;
		height = height < spinrArwHeight ? spinrArwHeight : height;
		reDraw();
		stateChanged(this);
	}

}
